/*
 * foc_declare.h
 *
 *  Created on: 2024年11月4日
 *      Author: zizhong
 */
#ifndef CONTROLLIB_DECLARE_H_
#define CONTROLLIB_DECLARE_H_

#include "PI.h"
#include "fcl_para.h"
#include "Enum.h"
#include "ramp.h"
#include "trans.h"
#include "svpwm.h"
#include "encoder.h"
#include "speed.h"

typedef struct _MOTOR_Vars_t_
{
    float posCnt;
    float posMaxCnt;

    float baseFreq;
    float polePairs;

    float Ts;
    float maxModIndex;          // Maximum module index

    float voltageLimit;         // limit voltage
    float currentLimit;         // limit current
    float speedRefStart;        // startup reference speed
    float speedRefMax;          // maximum reference speed
    float IdRefSet;             // Id reference setting (pu)
    float IqRefSet;             // Iq reference setting (pu)

    float IdRefPrev;            // Id reference previous setting (pu)
    float IqRefPrev;            // Iq reference previous setting (pu)
    float IdRefError;           // Id reference error (pu)
    float IqRefError;           // Iq reference error (pu)
    float IdRefDelta;           // Id reference delta (pu)
    float IqRefDelta;           // Iq reference delta (pu)

    float ctrlIdRef;            // Id reference (pu) to controller
    float ctrlIqRef;            // Iq reference (pu) to controller
    float ctrlSpeedRef;         // Speed reference (pu) to controller
    float ctrlPosRef;           // Position reference (pu) to controller

    float IdRef_start;          // Id reference (pu) for startup
    float IdRef_run;            // Id reference (pu) for running
    float IdRef;                // Id reference (pu)

    float IqRef_start;          // Iq reference (pu) for startup
    float IqRef;                // Iq reference (pu)

    float speedRef;             // For Closed Loop tests
    float positionRef;          // For Position Loop tests

    float currentScale;         // current scaling cofficient
    float voltageScale;         // voltage scaling cofficient
    float adcScale;             // ADC scale for current and voltage

    float posElecTheta;
    float posMechTheta;
    float speedWe;
    float speedWePrev;
    float pangle;               // rotor angle
    uint32_t absPos;
    float absPuTheta;

    float offset_currentAs;
    float offset_currentBs;
    float offset_currentCs;
    uint32_t offsetCalCounter;
    uint32_t offsetDoneFlag;

    float currentAs;            // phase A
    float currentBs;            // phase B
    float currentCs;            // phase C

    float   Vdcbus;             // DC bus voltage
    float   VdcbusMax;          // Maximum DC bus voltage
    float   VdcbusMin;          // Minimum DC bus voltage

    volatile uint32_t curA_PPBRESULT;
    volatile uint32_t curB_PPBRESULT;
    volatile uint32_t curC_PPBRESULT;
    volatile uint32_t volDC_PPBRESULT;

    uint32_t alignCnt;
    uint32_t alignCntMax;

    SVM_ctl_t SVM;



    RAMP_ctl_t rampController;                       // ramp control
    RAMP_gen_t rampGen;
    TRANS_clarke_t clarke;
    TRANS_ipark_t ipark;
    TRANS_park_t park;

    FCL_Parameters_t fclPara;
    FCL_cmplxCtrl_t cmplx_Id;
    FCL_cmplxCtrl_t cmplx_Iq;

    ENC_Status_e lsw;
    SPEED_MEAS_ABS_ENCODER speed;

    CtrlState_e  ctrlStateUse;
}MOTOR_Vars_t;



#endif /* CONTROLLIB_DECLARE_H_ */

